/**
 * Test script for the m3u8Downloader module
 */
const fs = require('fs');
const path = require('path');

async function testM3U8Module() {
  console.log('Testing m3u8Downloader module...');
  
  try {
    // Import the module
    const { downloadM3U8Manifest } = require('../src/utils/m3u8Downloader');
    
    // Test with the known media ID
    const mediaID = 'kCtDawsZ';
    console.log(`Downloading m3u8 manifest for media ID: ${mediaID}`);
    
    const m3u8Content = await downloadM3U8Manifest(mediaID);
    
    // Save to a test file
    const outputPath = path.join(__dirname, '..', 'test-m3u8-module-output.m3u8');
    fs.writeFileSync(outputPath, m3u8Content);
    
    console.log(`Successfully downloaded m3u8 manifest. Saved to: ${outputPath}`);
    
    // Show a preview of the content
    console.log('Content preview:');
    console.log(m3u8Content.substring(0, 500) + (m3u8Content.length > 500 ? '...' : ''));
    
    // Parse to show available streams
    console.log('\nAvailable streams:');
    const lines = m3u8Content.split(/\r?\n/);
    for (let i = 0; i < lines.length; i++) {
      if (lines[i].match("#EXT-X-STREAM-INF")) {
        console.log(`Stream info: ${lines[i]}`);
        if (i + 1 < lines.length) {
          console.log(`Stream URL: ${lines[i + 1]}`);
        }
      }
    }
    
    console.log('\nTest completed successfully!');
  } catch (error) {
    console.error('Test failed:', error.message);
    process.exit(1);
  }
}

// Run the test
testM3U8Module();